//
//  SEGMyActivityListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-2-26.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "ActivityListCell.h"
#import "ActivityRequestModel.h"
#import "SEGMyActivityListViewController.h"
#import "SEGOtherCreateActivityDetailViewController.h"
//#import "SEGSessionMessageViewController.h"

#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON)

@interface SEGMyActivityListViewController ()

@property (nonatomic, retain) ActivityRequestModel* activityRequestModel;

@property (nonatomic, retain) NSMutableArray* otherCreateArr;
@property (nonatomic, retain) NSMutableArray* myCreateArr;

@end

@implementation SEGMyActivityListViewController
@synthesize viewControllerDelegate, enterGruopid;
@synthesize listData;
@synthesize thumbImgView, activityID;
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)loadView
{
    UIScreen* scr = [UIScreen mainScreen];
    CGRect appbounds = CGRectMake(0, 0, scr.bounds.size.width, scr.bounds.size.height - kNavigationBarAndStatusBarHeight);
    UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appbounds.size.width, appbounds.size.height)];

    UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appbounds.size.width, 40)];
    headView.backgroundColor = [UIColor clearColor];
    float x = appbounds.size.width / 2;
    [mainView addSubview:headView];

    joinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    joinButton.frame = CGRectMake(0, 0, x, 40);
    [joinButton setTitleColor:kColor_T forState:UIControlStateNormal];
    joinButton.selected = YES;
    [joinButton setTitle:@"我参加的" forState:UIControlStateNormal];
    [joinButton setBackgroundColor:[UIColor whiteColor]];
    [joinButton.titleLabel setFont:[UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE]];
    [joinButton addTarget:self action:@selector(joinAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:joinButton];

    creatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    creatButton.frame = CGRectMake(x, 0, x, 40);
    [creatButton setTitleColor:kColor_2 forState:UIControlStateNormal];
    [creatButton setTitle:@"我发起的" forState:UIControlStateNormal];
    [creatButton setBackgroundColor:[UIColor whiteColor]];

    creatButton.selected = YES;
    [creatButton.titleLabel setFont:[UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE]];
    [creatButton addTarget:self action:@selector(creatAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:creatButton];

    CGRect cellBounds = CGRectMake(0, 39.5, appbounds.size.width, 0.5);
    UIImageView* linebg = [[UIImageView alloc] initWithFrame:cellBounds];
    linebg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
    [headView addSubview:linebg];

    lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(joinButton.center.x - 28, 37.5, 56, 2)];
    lineImage.backgroundColor = kColor_T;
    [headView addSubview:lineImage];

    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, appbounds.size.width, appbounds.size.height - 40) style:UITableViewStylePlain];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    m_tableView.backgroundView = nil;
    m_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    m_tableView.backgroundColor = [UIColor whiteColor];
    [m_tableView registerClass:[ActivityListCell class] forCellReuseIdentifier:kActivityListCellIdentifier];

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(mainView.bounds), 44)];
    headerView.backgroundColor = [UIColor clearColor];
    // m_tableView.tableHeaderView = headerView;
    [mainView addSubview:m_tableView];

    self.view = mainView;
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(requestOtherCreateStartingListActivity)
                                                 name:REQUEST_ACTIVITY_JOIN_SUCCESS
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(requestMyCreateStartingListActivity)
                                                 name:REQUEST_ACTIVITY_CREATE_SUCCESS
                                               object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.enterGruopid = @"";
    [self refreshData];
}
#pragma mark - 加载数据
//加载数据
- (void)refreshData
{
    if (joinButton.selected) {
        //        [[BusinessManager sharedManager].activityEntityManager requestOtherCreateStartingActivityList];
        [self requestOtherCreateStartingListActivity];
    }
    else {
        //        [[BusinessManager sharedManager].activityEntityManager requestMyCreateActivityListForStarting];
        [self requestMyCreateStartingListActivity];
    }
}

//我参加的
- (void)joinAction:(UIButton*)sender
{
    //    [[BusinessManager sharedManager].activityEntityManager requestOtherCreateStartingActivityList];
    [self requestOtherCreateStartingListActivity];
    joinButton.selected = YES;
    creatButton.selected = NO;
    [UIView animateWithDuration:0.25 animations:^{
        lineImage.frame = CGRectMake(joinButton.center.x - 28, 37.5, 56, 2);
    }];
    [joinButton setTitleColor:kColor_T forState:UIControlStateNormal];
    [creatButton setTitleColor:kColor_2 forState:UIControlStateNormal];
}

//我发起的
- (void)creatAction:(UIButton*)sender
{
    //    [[BusinessManager sharedManager].activityEntityManager requestMyCreateActivityListForStarting];
    [self requestMyCreateStartingListActivity];
    joinButton.selected = NO;
    creatButton.selected = YES;

    [UIView animateWithDuration:0.25 animations:^{
        lineImage.frame = CGRectMake(creatButton.center.x - 28, 37.5, 56, 2);
    }];
    [joinButton setTitleColor:kColor_2 forState:UIControlStateNormal];
    [creatButton setTitleColor:kColor_T forState:UIControlStateNormal];
}

- (void)requestOtherCreateStartingListActivity
{
    [self.activityRequestModel requestOtherCreateStartingListActivity:^(id result, NSError* error) {
        [self dealWithOtherCreateActivityResult:result];
    }];
}

- (void)requestMyCreateStartingListActivity
{
    [self.activityRequestModel requestMyCreateStartingListActivity:^(id result, NSError* error) {
        [self dealWithMyCreateActivityResult:result];
    }];
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.listData.count;
}

//每行的高度
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{

    return 80;
}
#pragma mark - tableview delegate
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSInteger row = indexPath.row;

    ActivityListCell* cell = [ActivityListCell cellWithTableView:tableView];
    cell.dataSource = [self.listData objectAtIndex:indexPath.row];

    //每个section最后一个活动 线和屏幕等宽
    if (row == self.listData.count - 1) {
        cell.isLastRow = YES;
    }
    else {
        cell.isLastRow = NO;
    }

    return cell;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ActivityModel* entity = [self.listData objectAtIndex:indexPath.row];
    [viewControllerDelegate pushViewController:[[SEGUhomeMediator sharedInstance]seg_initOtherCreateActivityDetailViewController:@{@"data":entity}]];
}

#pragma mark - 处理网络请求数据
//  我参与的活动
- (void)dealWithOtherCreateActivityResult:(NSDictionary*)result
{
    if (isRequestNew) {
        isRequestNew = NO;
    }

    NSString* code = [result objectForKey:@"code"];

    if ([code isEqualToString:@"0"]) {
        // 解析数据到model
        [self.otherCreateArr removeAllObjects];
        for (NSDictionary* item in [result objectForKey:@"data"]) {
            ActivityModel* model = [ActivityModel initWithDictionary:item];
            [self.otherCreateArr addObject:model];
        }

        if (joinButton.selected) {
            self.listData = self.otherCreateArr;
            if ([self.listData count] > 0) {
                m_tableView.backgroundColor = kColor_bg;
                [[SEGUhomeMediator sharedInstance]seg_hideCustomProgressImage:@{@"view":m_tableView}];
            }
            else {
                m_tableView.backgroundColor = [UIColor clearColor];
                [[SEGUhomeMediator sharedInstance]seg_showCustomProgressImage:@{@"view":m_tableView}];
            }
            [m_tableView reloadData];
        }
    }
}

// 我创建的活动
- (void)dealWithMyCreateActivityResult:(NSDictionary*)result
{
    if (isRequestNew) {
        isRequestNew = NO;
    }

    NSString* code = [result objectForKey:@"code"];

    if ([code isEqualToString:@"0"]) {
        // 解析数据到model
        [self.myCreateArr removeAllObjects];
        for (NSDictionary* item in [result objectForKey:@"data"]) {
            ActivityModel* model = [ActivityModel initWithDictionary:item];
            [self.myCreateArr addObject:model];
        }

        if (creatButton.selected) {
            self.listData = self.myCreateArr;
            if ([self.listData count] > 0) {
                m_tableView.backgroundColor = kColor_bg;
                [[SEGUhomeMediator sharedInstance]seg_hideCustomProgressImage:@{@"view":m_tableView}];
            }
            else {
                m_tableView.backgroundColor = [UIColor clearColor];
                [[SEGUhomeMediator sharedInstance]seg_showCustomProgressImage:@{@"view":m_tableView}];
            }
            [m_tableView reloadData];
        }
    }
}

#pragma mark - getter
- (ActivityRequestModel*)activityRequestModel
{
    if (!_activityRequestModel) {
        _activityRequestModel = [[ActivityRequestModel alloc] init];
    }
    return _activityRequestModel;
}

- (NSMutableArray*)otherCreateArr
{
    if (!_otherCreateArr) {
        _otherCreateArr = [[NSMutableArray alloc] init];
    }
    return _otherCreateArr;
}

- (NSMutableArray*)myCreateArr
{
    if (!_myCreateArr) {
        _myCreateArr = [[NSMutableArray alloc] init];
    }
    return _myCreateArr;
}

#pragma mark
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
